from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.spus_serializer import GoodsSPUSerialzier, CategorieSerialzier, BrandSerialzier
from meiduo_admin.utils import PageNum
from goods.models import SPU, GoodsCategory, Brand


class SPUSViewSet(ModelViewSet):
    serializer_class = GoodsSPUSerialzier
    queryset = SPU.objects.all().order_by('id')
    pagination_class = PageNum

    def brands(self, request):
        '''获取品牌'''
        data = Brand.objects.all().order_by('id')
        ser = BrandSerialzier(data, many=True)
        return Response(ser.data)

    def channel(self, request):
        # 1、获取一级分类数据
        data = GoodsCategory.objects.filter(parent=None).order_by('id')
        # 2、序列化返回分类数据
        ser = CategorieSerialzier(data, many=True)
        return Response(ser.data)

    def channels(self, request, pk):
        # 1、获取二级和三级分类数据
        data = GoodsCategory.objects.filter(parent_id=pk).order_by('id')
        # 2、序列化返回分类数据
        ser = CategorieSerialzier(data, many=True)
        return Response(ser.data)
